package com.example.gatewayserver.service.lock;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class SyncLockService {
    static ConcurrentHashMap<String, Boolean> map = new ConcurrentHashMap();

    //同步锁
    public static boolean getSyncLock(String applicationId) {
        if (map.get(applicationId) == null) {
            synchronized (applicationId) {
                if (map.get(applicationId) == null){
                    map.put(applicationId, true);
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            }
        }
        return false;
    }



    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String lock = "cxm_123" + "faceinterface";
                if(getSyncLock(lock)){
                    System.out.println(lock + " 胜出线程：" + Thread.currentThread().getName());
                }

            }).start();
        }


    }




}
